<?php
	class Statistic extends MY_Controller{
	
		public $data = array();
		
		public function __construct(){
			parent::__construct();
			$this->load->model('fund_model');
			$this->load->model('kiosk_model');
		}
		
		public function index(){
			$this->data['title'] 			= "All Total KIOSK Donate";			
			$this->get_chart();
		}		
		
		public function device($kid=FALSE){
			
			// in case link from kiosk list to chart
			if($kid!=FALSE)
				$_POST['kid'] = $kid;
			
			// load kiosk with total donate
			$arr_data				= $this->fund_model->get_total_fund_list(1);
			
			if(count($arr_data)>0){
				$arr = array();
				$arr_title = array('KIOSK','Donate');
				array_push($arr, $arr_title);
				
				foreach($arr_data as $v){
					$arr_donate = array($v['name'], intval($v['total_donate']));
					array_push($arr, $arr_donate);
					$this->data['chart_filter']	= json_encode($arr);	
				}
			}			
			$this->data['device']	= $this->input->post('kid');				
			$this->data['title'] 	= "KIOSK Statistic";
			$this->data['chart_filter_title'] 	= "Kiosk Chart";
			$this->get_chart();
		}
		
		public function time(){
			$this->load->library('form_validation');
			$this->form_validation->set_rules('start_date', 'Start date', 'callback_date_check');
			
			$this->data['start_date']	= $this->input->post('start_date');
			$this->data['end_date']		= $this->input->post('end_date');
			
			$arr = array();
			$arr_title = array('Date','Donate');
			array_push($arr, $arr_title);
			
			if ($this->form_validation->run() === FALSE){
				$arr_data				= $this->fund_model->get_total_fund_list();
				foreach($arr_data as $v){
					$arr_donate = array($v['name'], intval($v['total_donate']));
					array_push($arr, $arr_donate);
				}
							
				$this->data['chart']				= json_encode($arr);		
				$this->data['title'] 				= "All Total KIOSK Donate";
				$this->get_chart();
			}
			else{
				$arr_data				= $this->fund_model->get_total_fund_list(2);
				if(count($arr_data)>0){
					foreach($arr_data as $v){
						$arr_donate = array(date('m-d', strtotime($v['date'])), intval($v['total_donate']));
						array_push($arr, $arr_donate);
					}					
					$this->data['chart_filter']	= json_encode($arr);	
				}
				$this->data['title'] 				= "KIOSK Statistic";
				$this->data['chart_filter_title'] 	= "Kiosk chart by time";
				$this->data['chart_type'] 		= "line";
				$this->get_chart();
			}
		}
		
		public function get_chart(){
			// load the for helper
			$this->load->helper("form");
			
			// load all kiosk
			$this->data['kiosk']	= $this->kiosk_model->get_kiosk_list();
			
			// load all kiosk with total donate
			$arr_data				= $this->fund_model->get_total_fund_list();
			
			if(count($arr_data)>0){
				$arr = array();
				$arr_title = array('KIOSK','Donate');
				array_push($arr, $arr_title);
				foreach($arr_data as $v){
					$arr_donate = array($v['name'], intval($v['total_donate']));
					array_push($arr, $arr_donate);
				}
				$this->data['chart']	= json_encode($arr);
			}					
			
			$this->data['title_chart'] 	= "All Total KIOSK Donate";		
			
			$this->load->view('templates/header1', $this->data);
			$this->load->view('statistic/index', $this->data);
			$this->load->view('templates/right', $this->data);
			$this->load->view('templates/footer');
		}
		
		/**
		* date_check(): 	This method is defined rule for checking start date and end date
		*					Action:
		*						+ Check the start date and end date is required
		*						+ Check the start date should have to be lower than end date
		* 
		* @param    	
		* @return    		boolean
		* @access public
		*/
		public function date_check($start_date){
			
			// Get data form input form field
			$chk1 = trim($this->input->post('start_date'));
			$chk2 = trim($this->input->post('end_date'));
			
			// Required validation
			if(empty($chk1) || empty($chk2)){
				$this->form_validation->set_message('date_check', 'The start date and end date is required');
				return false;
			}
			
			
			// Compare date validation
			if(strtotime($chk1) > strtotime($chk2)){
				$this->form_validation->set_message('date_check', 'End date should be greater start date');
				return FALSE;
			}
			else return TRUE;
		}
		
	}
?>